package com.fantasy.service.impl;

import com.fantasy.dao.FavoriteDao;
import com.fantasy.dao.HistoryDao;
import com.fantasy.dao.StarDao;
import com.fantasy.dao.VideoDao;
import com.fantasy.dao.impl.FavoriteDaopImpl;
import com.fantasy.dao.impl.HistoryDaoImpl;
import com.fantasy.dao.impl.StarDaoImpl;
import com.fantasy.dao.impl.VideoDaoImpl;
import com.fantasy.entity.*;
import com.fantasy.service.VideoService;

import java.util.List;

public class VideoServiceImpl implements VideoService {
    VideoDao videoDao = new VideoDaoImpl();
    FavoriteDao favoriteDao = new FavoriteDaopImpl();
    HistoryDao historyDao = new HistoryDaoImpl();
    StarDao starDao = new StarDaoImpl();

    @Override
    public int insert(Video video) {
        return videoDao.insert(video);
    }

    @Override
    public int upDate(Video video) {
        return videoDao.upDate(video);
    }

    @Override
    public int delete(Long videoID) {
        return videoDao.delete(videoID);
    }

    @Override
    public List<Video> queryAll() {
        return videoDao.queryAll();
    }

    @Override
    public Video queryById(Long videoID) {
        return videoDao.queryById(videoID);
    }

    @Override
    public Page<Video> page(int pageNo, int pageSize) {
        Page<Video> videoPage = new Page<>();
        //设置当前分页对象的页码
        videoPage.setPageNo(pageNo);
        //设置当前页面的记录数
        videoPage.setPageSize(pageSize);
        //获取总记录条数
        Integer count = videoDao.queryForPageTotalCount();
        //设置总记录条数
        videoPage.setPageTotalCount(count);
        //设置该页面对象封装的记录集合
        List<Video> videos = videoDao.queryForPageItems((pageNo - 1) * pageSize, pageSize);
        videoPage.setItems(videos);
        //设置总页数
        int pageTotal = count / pageSize;
        if (count % pageSize > 0) {
            pageTotal++;
        }
        videoPage.setPageTotal(pageTotal);
        return videoPage;
    }

    @Override
    public Page<Video> pulley(int pageNo, int pageSize, int typeId) {
        Page<Video> videoPage = new Page<>();
        //设置当前分页对象的页码
        videoPage.setPageNo(pageNo);
        //设置当前页面的记录数
        videoPage.setPageSize(pageSize);
        //获取总记录条数
         Integer count = videoDao.videTypeCount(typeId);
        //设置总记录条数
        videoPage.setPageTotalCount(count);
        //设置该页面对象封装的记录集合
        List<Video> videos = videoDao.pulley((pageNo - 1) * pageSize, pageSize,typeId);
        videoPage.setItems(videos);
        //设置总页数
        int pageTotal = count / pageSize;
        if (count % pageSize > 0) {
            pageTotal++;
        }
        videoPage.setPageTotal(pageTotal);
        return videoPage;
    }

    @Override
    public Page<Video> ResultPage(String videoName, int pageNo, int pageSize) {
        Page<Video> videoPage = new Page<>();
        //设置当前分页对象的页码
        videoPage.setPageNo(pageNo);
        //设置当前页面的记录数
        videoPage.setPageSize(pageSize);
        //获取总记录条数
        Integer count = videoDao.queryForResultPageTotalCount(videoName);
        //设置总记录条数
        videoPage.setPageTotalCount(count);
        //设置该页面对象封装的记录集合
        List<Video> videos = videoDao.queryForResultPageItems(videoName,(pageNo - 1) * pageSize, pageSize);
        videoPage.setItems(videos);
        //设置总页数
        int pageTotal = count / pageSize;
        if (count % pageSize > 0) {
            pageTotal++;
        }
        videoPage.setPageTotal(pageTotal);
        return videoPage;
    }

    @Override
    public int favorite_insert(Favorites favorite) {
        return favoriteDao.insert(favorite);
    }

    @Override
    public int favorite_delete(Long video_id) {
        return favoriteDao.delete(video_id);
    }

    @Override
    public Page<Video> FavoritePage(int user_id, int pageNo, int pageSize) {
        Page<Video> videoPage = new Page<>();
        //设置当前分页对象的页码
        videoPage.setPageNo(pageNo);
        //设置当前页面的记录数
        videoPage.setPageSize(pageSize);
        //获取总记录条数
        Integer count = favoriteDao.queryForPageTotalCount(user_id);
        //设置总记录条数
        videoPage.setPageTotalCount(count);
        //设置该页面对象封装的记录集合
        List<Video> videos = favoriteDao.queryForPageItems(user_id,(pageNo - 1) * pageSize, pageSize);
        videoPage.setItems(videos);
        //设置总页数
        int pageTotal = count / pageSize;
        if (count % pageSize > 0) {
            pageTotal++;
        }
        videoPage.setPageTotal(pageTotal);
        return videoPage;
    }

    @Override
    public int history_insert(History history) {
        return historyDao.insert(history);
    }

    @Override
    public int history_delete(Long video_id) {
        return historyDao.delete(video_id);
    }

    @Override
    public Page<Video> HistroyPage(int user_id, int pageNo, int pageSize) {
        Page<Video> videoPage = new Page<>();
        //设置当前分页对象的页码
        videoPage.setPageNo(pageNo);
        //设置当前页面的记录数
        videoPage.setPageSize(pageSize);
        //获取总记录条数
        Integer count = historyDao.queryForPageTotalCount(user_id);
        //设置总记录条数
        videoPage.setPageTotalCount(count);
        //设置该页面对象封装的记录集合
        List<Video> videos = historyDao.queryForPageItems(user_id,(pageNo - 1) * pageSize, pageSize);
        videoPage.setItems(videos);
        //设置总页数
        int pageTotal = count / pageSize;
        if (count % pageSize > 0) {
            pageTotal++;
        }
        videoPage.setPageTotal(pageTotal);
        return videoPage;
    }

    @Override
    public List<Video> queryByUserId(String userId) {
        return videoDao.queryByUserId(userId);
    }

    @Override
    public int insert(Star star) {
        return starDao.insert(star);
    }

    @Override
    public Star queryByUserIdAndVideoId(Integer userId, Integer videoId) {
        return starDao.queryByUserIdAndVideoId(userId, videoId);
    }

    @Override
    public Integer queryForUserVideoTotalCount(int userId) {
        return videoDao.queryForUserVideoTotalCount(userId);
    }

    @Override
    public Favorites queryFavoritesByUserIdAndVideoId(Integer userId, Integer videoId) {
        return favoriteDao.queryFavoritesByUserIdAndVideoId(userId, videoId);
    }
}
